package ru.acme.lab14.domain.client;

import java.io.Serializable;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.StringTokenizer;
import ru.acme.lab14.domain.account.Account;
import ru.acme.lab14.domain.account.CheckingAccount;
import ru.acme.lab14.domain.account.ImmutableAccount;
import ru.acme.lab14.domain.account.SavingAccount;

public class Client implements ImmutableClient, Serializable {
	private static final long serialVersionUID = 5880143346993893009L;
	private List<Account> accounts;
	private String name;
	private Gender gender;
	
	public static Client parseClient(String string) throws ParseException {
		
		String[] fieldsNames = new String[] {"accounttype", "balance", "overdraft", "name", "gender"};
		String[] fieldsValues = new String[fieldsNames.length];
		StringTokenizer tokenizer = new StringTokenizer(string, ";", false);

		int i = 0;
		try {
			for (; i < fieldsNames.length; ++i) {
				String token = tokenizer.nextToken();
				if ((fieldsNames[i].equals("overdraft")) && (fieldsValues[0].equals("s"))) {
					if (!token.equals("-")) {
						throw new ParseException(string , i);
					}
					continue;
				}
				int equalPosition = token.indexOf('='); 
				if (equalPosition == -1) {
					throw new ParseException(string , i);
				}
				if (!fieldsNames[i].equals(token.substring(0, equalPosition))) {
					throw new ParseException(string , i);
				}
				fieldsValues[i] = token.substring(equalPosition + 1);		
			}
		} catch (NoSuchElementException e) {
			throw new ParseException(string , i);
		}
		if (tokenizer.hasMoreTokens()) {
			throw new ParseException(string, fieldsNames.length);
		}
			
		double balance, overdraft;
		Gender gender;
		String name;
		Account account;
		
		try {
			balance = Double.parseDouble(fieldsValues[1]);
		} catch (NumberFormatException e) {
			throw new ParseException(string, 1);
		}
		if (fieldsValues[0].equals("s")) {
			account = new SavingAccount(balance);
		} else if (fieldsValues[0].equals("c")) {
			if (fieldsValues[2] == null) {
				throw new ParseException(string, 2);
			}
			try {
				overdraft = Double.parseDouble(fieldsValues[2]);
			} catch (NumberFormatException e) {
				throw new ParseException(string, 2);
			}
			account = new CheckingAccount(balance, overdraft);
		} else {
			throw new ParseException(string, 0);
		}
		name = fieldsValues[3];
		if (fieldsValues[4].equals("m")) {
			gender = Gender.MALE;
		} else if (fieldsValues[4].equals("f")) {
			gender = Gender.FEMALE;
		} else {
			throw new ParseException(string, 4);
		}	
		
		return new Client(name, gender, account);
	}
	
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((accounts == null) ? 0 : accounts.hashCode());
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		result = prime * result + ((gender == null) ? 0 : gender.hashCode());
		return result;
	}

	public String toString() {
		StringBuffer stringBuffer = new StringBuffer('[');
		stringBuffer.append( "Client: name=" + name + ", gender="	+ gender + ", accounts: ");
		for (Account account : accounts) {
			stringBuffer.append('(');
			stringBuffer.append(account.toString());
			stringBuffer.append("), ");
		}
		stringBuffer.delete(stringBuffer.length() - 2, stringBuffer.length());
		return stringBuffer.toString();
	}

	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Client other = (Client) obj;
		if (accounts == null) {
			if (other.accounts != null)
				return false;
		} else if (!accounts.equals(other.accounts))
			return false;
		if (gender != other.gender)
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return true;
	}

	public Client(String name, Gender gender, Account account) {
		this.name = name;
		this.gender = gender;
		accounts = new ArrayList<Account>();
		accounts.add(account);
	}

	public List<? extends ImmutableAccount> getImmutableAccounts() {
		return Collections.unmodifiableList(accounts);
	}

	public List<Account> getAccounts() {
		return Collections.unmodifiableList(accounts);
	}

	public String getName() {
		return name;
	}

	public Gender getGender() {
		return gender;
	}
	
	public String getClientSalutation() {
		return gender.getSalutation() + " " + name;
	}
	
	public void addAccount(Account account) {
		accounts.add(account);
	}
}
